package cn.tedu.superscholarstore.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("/upload")
    public String upload(MultipartFile pic ) throws IOException {
    String fileName = pic.getOriginalFilename();
    String suffix = fileName.substring(fileName.lastIndexOf("."));
    fileName = UUID.randomUUID()+suffix;
    File dirFile = new File(dirPath);
    if(!dirFile.exists()){
        dirFile.mkdir();
    }
    String filePath = dirPath+"/"+fileName;
    pic.transferTo(new File(filePath));
    return "/"+fileName;
    }

    @RequestMapping("/remove")
    public void remove(String url){
        new File(dirPath+url).delete();
    }
}
